package controllers

import dao._
import implicits.Implicits._
import play.api.cache.AsyncCacheApi
import play.api.libs.json.Json
import play.api.mvc._
import shared.Pojo.CellData
import tool.FormTool
import ujson.play.PlayJson
import utils.Utils

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import upickle.default._

/**
 * Created by yz on 16/9/2021
 */
class TpmController @Inject()(cc: ControllerComponents, cache: AsyncCacheApi)(
  implicit val tpmDao: TpmDao,
  implicit val annoDao: AnnoDao,
) extends AbstractController(cc) {

  def getByGeneId = Action.async { implicit request =>
    val data = FormTool.geneIdForm.bindFromRequest().get
    annoDao.selectByGeneId(data.geneId).flatMap { x =>
      tpmDao.selectByGeneName(x.geneName).map { y =>
        Ok(writeJs(y))
      }
    }
  }

  def all = Action.async { implicit request =>
    tpmDao.selectAll.map { x =>
      val array = x.map { y =>
        writeJs(y)
      }
      Ok(writeJs(array))
    }
  }

  def allByServer = Action.async { implicit request =>
    val obj = PlayJson.transform(request.body.asJson.get, reader[ujson.Obj])
    val pageData = FormTool.pageForm.bindFromRequest().get
    tpmDao.selectAll(obj.value, pageData).map { case (size, x) =>
      val array = writeJs(x)
      Ok(ujson.Obj("rows" -> array, "total" -> size))
    }
  }

}
